﻿Imports System.Data.OleDb
Imports LecturerAppointmentSystem.UserDetails

Public Class Appointments
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lbl_TodaysDate.Text = Date.Today
        If Request.QueryString("ref") = "manage" Then
            lbl_msg.Visible = True
        End If
        'If User.IsInRole("Lecturer") Then
        'AppointmentsMultiView.SetActiveView(LecturerView)
        'ElseIf User.IsInRole("Student") Then
        '    AppointmentsMultiView.SetActiveView(StudentView)
        'ElseIf User.IsInRole("Administrator") Then
        '    AppointmentsMultiView.SetActiveView(AdministratorView)
        If User.IsInRole("Student") Then
            lb_BookAppointment.Visible = True
            AppointmentsMultiView.SetActiveView(StudentView)
        Else
            lb_BookAppointment.Visible = False
            AppointmentsMultiView.SetActiveView(LecturerView)
        End If
        LecturerGridView.DataBind()
        StudentGridView.DataBind()
    End Sub

    Protected Function changeText(ByVal place As Object) As String
        Dim dbValue As String
        If IsDBNull(place) Then
            dbValue = ""
        Else
            dbValue = place.ToString().Trim()
        End If
        Return (If(dbValue = "", "Mark As Available", "Mark As Unavailable"))
    End Function

    Protected Function parseBooked(ByVal booked As Object) As String
        Dim dbValue As String = ""
        If IsDBNull(booked) Then
            dbValue = "N/A"
        ElseIf booked = "False" Then
            dbValue = "Not Booked"
        ElseIf booked = "True" Then
            dbValue = "Booked"
        End If
        Return dbValue
    End Function

    Protected Function getStatus(ByVal booked As Object) As String
        Dim dbValue As String = ""
        If IsDBNull(booked) Then
            dbValue = "No Appointment Scheduled"
        ElseIf booked = "False" Then
            dbValue = "No Appointment Scheduled"
        ElseIf booked = True Then
            dbValue = "Appointment Scheduled"
        End If
        Return dbValue
    End Function

    Protected Function getVisibility(ByVal booked As Object) As Boolean
        Dim visible As Boolean = False
        If IsDBNull(booked) Then
            visible = False
        ElseIf booked = "False" Then
            visible = False
        ElseIf booked = True Then
            visible = True
        End If
        Return visible
    End Function

    Function isAppointmentMarkedAsAvailable(ByVal clickedSlot As Integer) As Boolean
        Dim appointment As Boolean = False
        Dim sql As String = "SELECT * FROM Appointments WHERE AppointmentDate = @AppointmentDate AND Fkid_TimeSlot = @Fkid_TimeSlot AND Lecturer = @Lecturer"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@AppointmentDate", Request.QueryString("Date"))
        cmd.Parameters.AddWithValue("@Fkid_TimeSlot", clickedSlot)
        cmd.Parameters.AddWithValue("@Lecturer", Request.QueryString("UserId"))
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        If reader.Read() Then
            appointment = True
        End If
        conn.Close()
        Return appointment
    End Function

    Function isAppointmentBooked(ByVal selectedDate As String, ByVal timeSlot As Integer)
        Dim booked As Boolean = False
        Dim sql As String = "SELECT * FROM Appointments WHERE AppointmentDate=@AppointmentDate AND Fkid_TimeSlot=@Fkid_TimeSlot AND Lecturer=@Lecturer"
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@AppointmentDate", selectedDate)
        cmd.Parameters.AddWithValue("@Fkid_TimeSlot", timeSlot)
        cmd.Parameters.AddWithValue("@Lecturer", Request.QueryString("UserId"))
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        If reader.Read() Then
            booked = reader("Booked").ToString()
        End If
        conn.Close()
        Return booked
    End Function

    'Protected Sub ImgBtn_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
    '    Dim btn_details As ImageButton = CType(sender, ImageButton)
    '    Dim row As GridViewRow = TryCast(btn_details.NamingContainer, GridViewRow)
    '    lbl_TimeSlot.Text = row.RowIndex.ToString()
    '    Dim timeSlot As Integer = CType(lbl_TimeSlot.Text, Integer) + 1
    '    If (isAppointmentMarkedAsAvailable(timeSlot)) Then
    '        If isAppointmentBooked(Request.QueryString("Date"), timeSlot) Then
    '            CancelBookingPopup.Show()
    '        Else
    '            UnavailablePopup.Show()
    '        End If
    '    Else
    '        AvailablePopup.Show()
    '    End If
    'End Sub

    'Protected Sub ButtonSave_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Dim timeSlot As Integer = CType(lbl_TimeSlot.Text, Integer) + 1
    '    If isAppointmentMarkedAsAvailable(timeSlot + 1) = False Then
    '        Dim sql As String = "INSERT INTO Appointments(AppointmentDate, Fkid_TimeSlot, Place, Agenda, Booked, Lecturer) VALUES(@AppointmentDate, @Fkid_TimeSlot, @Place, @Agenda, @Booked, @Lecturer)"
    '        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
    '        conn.Open()
    '        cmd.Parameters.AddWithValue("@AppointmentDate", Request.QueryString("Date"))
    '        cmd.Parameters.AddWithValue("@Fkid_TimeSlot", timeSlot)
    '        cmd.Parameters.AddWithValue("@Place", tb_Place.Text)
    '        cmd.Parameters.AddWithValue("@Agenda", "")
    '        cmd.Parameters.AddWithValue("@Booked", 0)
    '        cmd.Parameters.AddWithValue("@Lecturer", Request.QueryString("UserId"))
    '        cmd.ExecuteNonQuery()
    '        conn.Close()
    '        LecturerGridView.DataBind()
    '    End If
    'End Sub

    'Protected Sub ButtonYes_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Dim AppointmentDate As Date = Request.QueryString("Date")
    '    Dim timeSlot As Integer = CType(lbl_TimeSlot.Text, Integer) + 1
    '    If isAppointmentBooked(AppointmentDate, timeSlot) Then
    '        CancelBookingPopup.Show()
    '    Else
    '        Dim sql As String = "DELETE * FROM Appointments WHERE AppointmentDate = @AppointmentDate AND Fkid_TimeSlot = @Fkid_TimeSlot AND Lecturer = @Lecturer"
    '        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
    '        cmd.Parameters.AddWithValue("@AppointmentDate", AppointmentDate)
    '        cmd.Parameters.AddWithValue("@Fkid_Timeslot", timeSlot)
    '        cmd.Parameters.AddWithValue("@Lecturer", Request.QueryString("UserId"))
    '        conn.Open()
    '        cmd.ExecuteNonQuery()
    '        conn.Close()
    '        LecturerGridView.DataBind()
    '    End If

    'End Sub

    'Protected Sub ButtonOkCancel_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Dim AppointmentDate As Date = Request.QueryString("Date")
    '    Dim timeSlot As Integer = CType(lbl_TimeSlot.Text, Integer) + 1
    '    Dim AppointmentId As Integer
    '    Dim sql As String = "SELECT ID FROM Appointments WHERE AppointmentDate = @AppointmentDate AND Fkid_TimeSlot = @Fkid_TimeSlot AND Lecturer = @Lecturer"
    '    Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
    '    cmd.Parameters.AddWithValue("@AppointmentDate", AppointmentDate)
    '    cmd.Parameters.AddWithValue("@Fkid_TimeSlot", timeSlot)
    '    cmd.Parameters.AddWithValue("@Lecturer", Request.QueryString("UserId"))
    '    conn.Open()
    '    Dim reader As OleDbDataReader = cmd.ExecuteReader()
    '    If reader.Read() Then
    '        AppointmentId = reader("ID")
    '    End If
    '    conn.Close()
    '    Response.Redirect("AppointmentDetails.aspx?ID=" & AppointmentId)
    'End Sub
End Class